package net.vanchu.mortgagecalculator.mortgage.calculate;

import net.vanchu.mortgagecalculator.common.config.LoanWay;
import net.vanchu.mortgagecalculator.mortgage.calculate.mix.MixLoanWayInputData;
import net.vanchu.mortgagecalculator.mortgage.calculate.single.SingleLoanWayInputData;
import net.vanchu.mortgagecalculator.mortgage.data.calculator.MortgageCalculator;
import net.vanchu.mortgagecalculator.mortgage.data.loan.ComLoan;
import net.vanchu.mortgagecalculator.mortgage.data.loan.MixLoan;
import net.vanchu.mortgagecalculator.mortgage.data.loan.ProLoan;
import net.vanchu.mortgagecalculator.mortgage.data.repaymentSummary.RepaySummary;

/**
 * 计算逻辑 Controller
 * <p>
 * Created by Granton on 2016/12/19.
 */
public class CalculatorHelper {

    private CalculatorHelper() {
    }

    private static class HOLDER {
        private static CalculatorHelper instance = new CalculatorHelper();
    }

    public static CalculatorHelper getInstance() {
        return HOLDER.instance;
    }

    public RepaySummary.Single calSingle(SingleLoanWayInputData singleLoanWayInputData) {
        if (singleLoanWayInputData.getCurrentLoanWay() == LoanWay.wayCommercial) {
            ComLoan comLoans = new ComLoan(
                    singleLoanWayInputData.getYear(),
                    singleLoanWayInputData.getValue() * 10000,
                    singleLoanWayInputData.getRate() / 100,
                    singleLoanWayInputData.getDiscount()
            );
            return MortgageCalculator.calSingle(comLoans);
        }
        ProLoan proLoans = new ProLoan(
                singleLoanWayInputData.getYear(),
                singleLoanWayInputData.getValue() * 10000,
                singleLoanWayInputData.getRate() / 100,
                singleLoanWayInputData.getDiscount()
        );
        return MortgageCalculator.calSingle(proLoans);
    }

    public RepaySummary.Mix calMix(MixLoanWayInputData mixLoanWayInputData) {
        MixLoan mixLoans = new MixLoan(mixLoanWayInputData.getYear(),
                mixLoanWayInputData.getComValue() * 10000,
                mixLoanWayInputData.getComRate() / 100,
                mixLoanWayInputData.getComDiscount(),
                mixLoanWayInputData.getProValue() * 10000,
                mixLoanWayInputData.getProRate() / 100,
                mixLoanWayInputData.getProDiscount()
        );
        return MortgageCalculator.calMix(mixLoans);
    }

}